package com.qc.bug.Controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.qc.bug.Services.bugInfoServices;
import com.qc.bug.Services.userSearchBugServices;
import com.qc.bug.Util.User;
import com.qc.bug.pojo.BugInfo;
import com.qc.bug.pojo.UserInfo;

@Controller


@RequestMapping(value="/buginfocontr")
public class bugInfoController {
	
	@Autowired
	private bugInfoServices bugInfoBiz;
	@Autowired
	private userSearchBugServices uSearchBugBiz;

	
	/**
	 * 添加bug信息
	 * @param buginfo
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/addbuginfo")
	public String addBugInfo(BugInfo buginfo,HttpSession session){
		UserInfo userinfo=(UserInfo) session.getAttribute(User.USER_INFO);
		buginfo.setAddpeopleid(userinfo.getUserid());
		
		bugInfoBiz.addBugInfo(buginfo);
		
		return "userindex";
	}
	
	/**
	 * 修改bug信息
	 * @param buginfo
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/changgebuginfo")
	public String changgeBugInfo(BugInfo buginfo,HttpSession session,Map<String,Object> map){
		UserInfo userinfo=(UserInfo) session.getAttribute(User.USER_INFO);
		buginfo.setModifierid(userinfo.getUserid());
		
		if(bugInfoBiz.updateBugInfo(buginfo)>0){
			map.put("result", "修改成功！");
		}else{
			map.put("result", "修改失败！");
		}
		
		return "resule/result";
	}
	
	/**
	 * 
	 * @param str
	 * @param map
	 * @return
	 * 
	 * 模糊查询 --检索
	 */
	@ResponseBody
	@RequestMapping(value="/findall",produces="text/html;charset=utf-8")
	public Object findAllBuginfo(@RequestParam String str){

		return JSON.toJSONString(bugInfoBiz.findAllBugInfo(str));
	}
	
	/**
	 * 通过bug问题描述模糊查询信息
	 * @param description
	 * @param pageIndex
	 * @param pageSize
	 * @param map
	 * @return
	 */
	@RequestMapping(value="findbydescription")
	public String findBydescription(@RequestParam String description,@RequestParam(defaultValue="1") int pageIndex,@RequestParam(defaultValue="3") int pageSize,Map<String,Object> map){
		map.put("description", description);
		map.put("pageIndex", pageIndex);
		map.put("pageNumber", bugInfoBiz.getPageNumber(description,pageSize));
		map.put("bugList", bugInfoBiz.findBydescription(description,pageIndex,pageSize));
		
		return "search";
	}
	
	
	/**
	 * 通过id查询buginfo 详情
	 * @param bugId
	 * @param map
	 * @return
	 */
	@RequestMapping(value="findbyid/{id}")
	public String findById(@PathVariable("id") int bugId,Map<String,Object> map,HttpSession session){
		UserInfo userinfo=(UserInfo) session.getAttribute(User.USER_INFO);
		map.put("buginfo", bugInfoBiz.findById(bugId));
		if(null!=userinfo){
			uSearchBugBiz.addSearchInfo(bugId,userinfo);
		}
		return "buginfo";
	}
	
	@RequestMapping(value="deletebyid/{id}")
	public String deleteBYId(@PathVariable("id") int bugId,Map<String,Object> map){
		int a=bugInfoBiz.deleteBYId(bugId);
		if(a>0){
			//删除查询表中对应的数据
			uSearchBugBiz.deleteBYId(bugId);
			map.put("result", "删除成功");	
		}else{
			map.put("result", "删除失败");
		}
		
		return "result/result";
	}
	
	
}
